#include "pagebattery.h"
#include "ui_pagebattery.h"

PageBattery::PageBattery(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageBattery)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    m_lBatStatNameList <<"蓄电池电压(V)"
                       <<"充电电流(A)"
                       <<"放电电流(A)"
                       <<"蓄电池剩余容量SOC"
                       <<"箱体温度(℃)"
                       <<"电压采样线断开编号"
                       <<"温度传感器故障编号"
                       <<"氢气浓度(%)";

    InitBatStatLabels();
    setDefaultBrkStat();
}

PageBattery::~PageBattery()
{
    delete ui;
}

void PageBattery::InitBatStatLabels()
{
    m_nStatLabelWidth_Car = MiniCarSaloonWidth+MiniCarHSpace_1220;
    m_nStatLabelSpaceX = MiniCarLeftSpace_1220 - MiniCarHSpace_1220/2;
    m_nStatLabelHeight = 45;
    m_nStatLabelStartY = 15;
    m_nStatLabelSpaceY = 0;

    uint8_t statCnt = m_lBatStatNameList.length();
    for(int i=0;i<statCnt;i++)
    {
        if(i%8 == 0)
        {
            QWidget *w = new QWidget(this);
            w->setGeometry(0,0,this->width(),this->height()-90);
            w->setVisible(i==0);
            m_lDisplayWidgets.append(w);
        }
        QLabel *statName = new QLabel(m_lDisplayWidgets[i/8]);
        statName->setStyleSheet(StatLabelStyle);
        statName->setAlignment(Qt::AlignCenter);
        statName->setText(m_lBatStatNameList.at(i));
        statName->setGeometry(m_nStatLabelSpaceX-190,
                              m_nStatLabelStartY+(i%8)*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                              190,m_nStatLabelHeight);

        uint8_t statOneCarCnt = 1;
        QList <QLabel *> OneCarList;
        OneCarList.clear();
        for(int j=0;j<SaloonCnt;j++)
        {
            for(int k=0;k<statOneCarCnt;k++)
            {
                QLabel *stat = new QLabel(m_lDisplayWidgets[i/8]);
                stat->setStyleSheet(StatLabelStyle);
                stat->setAlignment(Qt::AlignCenter);
                SetPosition(stat,CAR(j),ROW(i%8),COL(k),TOTAL(statOneCarCnt),j>1);
                OneCarList.append(stat);
            }
        }
        m_lBatStatList.append(OneCarList);
    }
}

void PageBattery::setDefaultBrkStat()
{
    for(int i=0;i<BAT_Cnt;i++)
    {
        setBatStat(i,0,0);
        setBatStat(i,5,0);
    }
}

//row 和 col 都从0开始
void PageBattery::SetPosition(QWidget* w,int car, uint8_t row, uint8_t col, uint8_t total, bool xReverse)
{
    uint OnestatLabelWidth = m_nStatLabelWidth_Car/total;
    float rowSpaceX = (m_nStatLabelWidth_Car - total*OnestatLabelWidth)/(total+1);
    if(!xReverse)
        w->setGeometry(m_nStatLabelSpaceX+rowSpaceX+car*m_nStatLabelWidth_Car+col*(OnestatLabelWidth+rowSpaceX),
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
    else
        w->setGeometry(m_nStatLabelSpaceX+rowSpaceX+car*m_nStatLabelWidth_Car+(total-1-col)*(OnestatLabelWidth+rowSpaceX),
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
};

void PageBattery::setBatStat(int item, int index, float stat)
{
   if(index >= m_lBatStatList[item].length())
        return;
   if(item <= BAT_DisChargeCurrent)
        m_lBatStatList[item][index]->setText(QString("%1").arg(stat*0.1));
   else if(item == BAT_SOC)
       m_lBatStatList[item][index]->setText(QString("%1 %").arg(stat));
   else if(item == BAT_BoxTemp)
       m_lBatStatList[item][index]->setText(QString("%1").arg((stat-550)*0.1));
   else if(item >= BAT_UnconNum && item <= BAT_TempFaultNum)
       m_lBatStatList[item][index]->setText(QString("%1").arg(stat));
   if(item == BAT_hydrogen)
        m_lBatStatList[item][index]->setText(QString("%1").arg(stat*0.1));
}
